//
//  CaogaoMainViewController.m
//  YiqiMobile
//
//  Created by henry on 13-4-21.
//  Copyright (c) 2013年 henry. All rights reserved.
//

#import "CaogaoMainViewController.h"
#import "CaogaoViewController.h"
#import "CaogaologViewController.h"
#import "Utility.h"
#import "Config.h"
#import "CaogaomanageViewController.h"
#import "CaogaoPreviewViewController.h"
@interface CaogaoMainViewController ()

@end

@implementation CaogaoMainViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    [self showSubMenu];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:[Utility customButton:self action:@selector(navButtonActon:) normalImage:@"cg_cannel_normal.png" lightedImage:@"cg_cannel_hiver.png" frame:CGRectMake(0, 0, 58, 29) title:nil tag:NAVBUTTONTAG+1]];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:[Utility customButton:self action:@selector(navButtonActon:) normalImage:@"cg_eidt_normal.png" lightedImage:@"cg_eidt_hover.png" frame:CGRectMake(0, 0, 58, 29) title:nil tag:NAVBUTTONTAG+2]];
}

-(void) initData:(NSDictionary*) param{
    self.param=param;
     self.navigationItem.titleView=[Utility getTitleView:[param objectForKey:@"str2"]];
    float height=379;
    if (iPhone5) {
        height+=88;
    }
    self.contentView=[[UIView alloc] initWithFrame:CGRectMake(0, 37, 320, height)];
    self.contentView.backgroundColor=[UIColor clearColor];
    [self.view addSubview:self.contentView];
    CaogaoViewController *caogaoViewController=[[CaogaoViewController alloc] initWithNibName:@"CaogaoViewController" bundle:nil];
    [self addChildViewController:caogaoViewController];
    CaogaologViewController *caogaologViewController=[[CaogaologViewController alloc] initWithNibName:@"CaogaologViewController" bundle:nil];
    caogaologViewController.caogaoId=[self.param objectForKey:@"str1"];
    [self addChildViewController:caogaologViewController];
    CaogaoPreviewViewController *caogaoPreviewViewController=[[CaogaoPreviewViewController alloc] initWithNibName:@"CaogaoPreviewViewController" bundle:nil];
    [self addChildViewController:caogaoPreviewViewController];
    
    [self.contentView addSubview:caogaoViewController.view];
    self.currentViewController=caogaoViewController;
    [caogaoViewController loadGonggaoDataFromServer:param];
    [caogaologViewController loaddataFromserverWIthurrpage:1];
    [caogaoPreviewViewController loadDataFromServer:[param objectForKey:@"str1"]andPage:1];
}

-(void) showSubMenu{
    
    [self.view addSubview:[Utility customButton:self action:@selector(onClickbutton:) normalImage:@"cg_allgg_normal.png" lightedImage:@"cg_allgg_hover.png" frame:CGRectMake(0, 0, 107, 37) title:nil tag:CAOGAOMAINBUTTONTAG+1]];
    [self.view  addSubview:[Utility customButton:self action:@selector(onClickbutton:) normalImage:@"cg_log_normal.png" lightedImage:@"cg_log_hover.png" frame:CGRectMake(107, 0, 107, 37) title:nil tag:CAOGAOMAINBUTTONTAG+2]];
    [self.view  addSubview:[Utility customButton:self action:@selector(onClickbutton:) normalImage:@"cg_preview_normal.png" lightedImage:@"cg_preview_hover.png" frame:CGRectMake(213, 0, 107, 37) title:nil tag:CAOGAOMAINBUTTONTAG+3]];
}


-(IBAction)onClickbutton:(id)sender{
    UIButton *selectButton=(UIButton*)sender;
    for (int i=1; i<4; i++) {
        UIButton *button=(UIButton*)[self.view viewWithTag:CAOGAOMAINBUTTONTAG+i];
        button.selected=NO;
    }
    selectButton.selected=YES;
    int tag=[sender tag]-CAOGAOMAINBUTTONTAG;
    CaogaoViewController *caogaoViewController=[self.childViewControllers objectAtIndex:0];
    CaogaologViewController *caogaologViewController=[self.childViewControllers objectAtIndex:1];
    CaogaoPreviewViewController *caogaoPreviewViewController=[self.childViewControllers objectAtIndex:2];
    if ((self.currentViewController==caogaoViewController&&tag==1)||(self.currentViewController==caogaologViewController&&tag==2) ||(self.currentViewController==caogaoPreviewViewController&&tag==3)) {
        return;
    }
    UIViewController *oldViewController=self.currentViewController;
    switch (tag) {
        case 1:{
            [self transitionFromViewController:self.currentViewController toViewController:caogaoViewController duration:0.5 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
            }  completion:^(BOOL finished) {
                if (finished) {
                    self.currentViewController=caogaoViewController;
                }else{
                    self.currentViewController=oldViewController;
                }
            }];
            break;
        }
            
        case 2:{
          //[caogaologViewController loaddataFromserverWIthurrpage:1];
            [self transitionFromViewController:self.currentViewController toViewController:caogaologViewController duration:0.5 options:UIViewAnimationOptionTransitionCurlDown animations:^{
                
            }  completion:^(BOOL finished) {
                if (finished) {
                    self.currentViewController=caogaologViewController;
                }else{
                    self.currentViewController=oldViewController;
                }
            }];
            break;
        }
            
        case 3: {
            [self transitionFromViewController:self.currentViewController toViewController:caogaoPreviewViewController duration:0.5 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
                
            }  completion:^(BOOL finished) {
                if (finished) {
                    self.currentViewController=caogaoPreviewViewController;
                }else{
                    self.currentViewController=oldViewController;
                }
            }];
            break;
        }
                   
    }
}
-(IBAction)navButtonActon:(UIButton*)sender{
    if (sender.tag==NAVBUTTONTAG+1) {
        [self.navigationController popViewControllerAnimated:YES];
    }else if(sender.tag==NAVBUTTONTAG+2){
        CaogaomanageViewController *caogaomanageViewController=[[CaogaomanageViewController alloc] initWithNibName:nil bundle:nil];
        [caogaomanageViewController initData:self.param];
        [self.navigationController  pushViewController:caogaomanageViewController animated:YES];
    }
    
}

- (void)didReceiveMemoryWarning

{
    
    [super didReceiveMemoryWarning];
    
    // Dispose of any resources that can be recreated.
    
}
@end